﻿from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404
from jinja2_shortcuts import render_to_response
from django.conf import settings
from django.utils.safestring import mark_safe
from yazasport.staticpages.models import StaticPage

DEFAULT_TEMPLATE = 'base.html'

def staticpage(request, url):
    if not url.endswith('/') and settings.APPEND_SLASH:
        return HttpResponseRedirect("%s/" % request.path)
    if not url.startswith('/'):
        url = "/" + url
        
    staticpage = get_object_or_404(StaticPage, url__exact=url)
    
    if staticpage.is_public:
        template_name = staticpage.template_name if staticpage.template_name else DEFAULT_TEMPLATE
        staticpage.header = mark_safe(staticpage.header)
        staticpage.content = mark_safe(staticpage.content)
        return render_to_response(template_name, {'staticpage': staticpage})
